﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace DBComparer
{
    public partial class Database : UserControl
    {
        string _ConnectionString = "";
        string _selectedDatabase = "";

        public string SelectedDatabase
        {
            get { return _selectedDatabase; }
            set { _selectedDatabase = value; }
        }
        public string ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value; }
        }

        public event EventHandler onDatabaseSelectedChange;
        public event EventHandler onDatabaseListLoaded;

        public void Reload(SqlConnection argConnection)
        {
            cbo.DisplayMember = cbo.ValueMember = "DATABASE_NAME";
            DataSet ds = new DataAccess.Databases().GetDatabases(argConnection);

            if (ds.Tables.Count > 0)
            {
                cbo.DataSource = ds.Tables[0];
                //cbo.SelectedIndex = -1;

                if (onDatabaseListLoaded!=null)
                    onDatabaseListLoaded(new object(), EventArgs.Empty);
            }
        }

        public Database()
        {
            InitializeComponent();
        }

        private void cbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo.SelectedIndex != -1)
            {
                SelectedDatabase = cbo.SelectedValue.ToString();

                if (onDatabaseSelectedChange != null)
                    onDatabaseSelectedChange(sender, e);
            }

        }

        private void Database_Load(object sender, EventArgs e)
        {
            this.cbo.SelectedIndexChanged += new System.EventHandler(this.cbo_SelectedIndexChanged);
        }
    }
}
